package com.jfree.zk.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangjian
 * @date 2019/7/16 17:16
 */
@Configuration
public class CuratorConfig {
    /**
     * 后期应该把这些参数配置到配置文件中
     */
    public static Logger log = LoggerFactory.getLogger(DistributedLock.class);
    private static String ZK_URL = "39.97.182.21:2181";
    private Integer baseSleepTimeMs=1000;
    private Integer maxRetries=3;

    @Bean(destroyMethod = "close")
    public CuratorFramework curatorFramework(){
        //session失效后，curatorFramework还能否正常使用？？
        /**这个问题解决一下
         * 比如用InterProcessMutex这个，
         * 会不会出现我再获取到a锁的时候，程序网络中断，a中的临时节点被删除了，然后其他程序来拿这个锁，发现自己创建的临时节点在a中最小，然后拿到锁？
         */
        CuratorFramework   curatorFramework = CuratorFrameworkFactory.newClient(ZK_URL, new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries));
        curatorFramework.start();
        return curatorFramework;
    }
    public static CuratorFramework getClient(){
        CuratorFramework   curatorFramework = CuratorFrameworkFactory.newClient(ZK_URL, new ExponentialBackoffRetry(1000, 3));
        curatorFramework.start();
        return curatorFramework;
    }
}
